﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        if (Session["PhanQuyen"].ToString() != "1")
        {
            Response.Redirect("quanly.aspx");
        }

        if (Request["act"] == null)
        {
            pnLoiChao.Visible = true;
            lbThongBao.Text = ""; 
            return;
        }
        
        if (Request["act"].ToString() == "trang-chu")
        {
            TrangChu tc = new TrangChuDAO().HienThiTrangChu()[0];
            edtNoiDung.Text = tc.NoiDung;
            lbTitle.Text = "TRANG CHỦ";
            lbThongBao.Text = "";
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = true;
            pnSlideShow.Visible = false;
            return;
        }
        if (Request["act"].ToString() == "gioi-thieu")
        {
            GioiThieu gt = new GioiThieuDAO().HienThiGioiThieu()[0];
            edtNoiDung.Text = gt.NoiDung;
            lbTitle.Text = "GIỚI THIỆU";
            lbThongBao.Text = "";            
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = true;
            pnDanhMuc.Visible = true;
            pnDanhMucCon.Visible = false;
            pnTinTuc.Visible = false;
            pnSlideShow.Visible = false;
            return;
        }
        if (Request["act"].ToString() == "lien-he")
        {
            LienHe gt = new LienHeDAO().HienThiLienHe()[0];
            edtNoiDung.Text = gt.NoiDung;
            lbTitle.Text = "LIÊN HỆ";
            lbThongBao.Text = "";
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = true;
            pnDanhMuc.Visible = true;
            pnDanhMucCon.Visible = false;
            pnTinTuc.Visible = false;
            pnSlideShow.Visible = false;
            return;
        }
        if (Request["act"].ToString() == "danh-muc")
        {
            lbTitle.Text = "DANH MỤC";
            lbThongBao.Text = "";
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = false;
            pnDanhMuc.Visible = true;
            pnDanhMucCon.Visible = false;
            pnTinTuc.Visible = false;
            pnSlideShow.Visible = false;
            grvDanhMuc.DataSource = new DanhMucDAO().HienThiDanhMuc();
            grvDanhMuc.DataBind();
            return;
        }
        if (Request["act"].ToString() == "tin-tuc")
        {
            lbTitle.Text = "TIN TỨC";
            lbThongBao.Text = "";
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = false;
            pnDanhMuc.Visible = false;
            pnDanhMucCon.Visible = false;
            pnTinTuc.Visible = true;
            pnSlideShow.Visible = false;
            grvTinTuc.DataSource = new TinTucDAO().HienThiTinTuc();
            grvTinTuc.DataBind();
            return;
        }
        if (Request["act"].ToString() == "slide-show")
        {
            lbTitle.Text = "SLIDESHOW";
            lbThongBao.Text = "";
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = false;
            pnDanhMuc.Visible = false;
            pnDanhMucCon.Visible = false;
            pnTinTuc.Visible = false;
            pnSlideShow.Visible = true;
            grvSlideShow.DataSource = new SlideShowDAO().HienThiSlideShow();
            grvSlideShow.DataBind();
            return;
        }
        if (Request["act"].ToString() == "lien-ket-web")
        {
            lbTitle.Text = "LIÊN KẾT WEB";
            lbThongBao.Text = "";
            pnLoiChao.Visible = false;
            pnTrangChu.Visible = false;
            pnDanhMuc.Visible = false;
            pnDanhMucCon.Visible = false;
            pnTinTuc.Visible = false;
            pnSlideShow.Visible = false;
            pnLienketWeb.Visible = true;
            grvLienKetWeb.DataSource = new LienKetWebDAO().HienThiLienKetWeb();
            grvLienKetWeb.DataBind();
            return;
        }
               
    }
    protected void btnTrangChu_Click(object sender, EventArgs e)
    {
        string noiDung = edtNoiDung.Text;
        int kq = 0;
        if (Request["act"].ToString() == "trang-chu")
            kq = new TrangChuDAO().CapNhatTrangChu(noiDung);
        else if (Request["act"].ToString() == "gioi-thieu")
            kq = new GioiThieuDAO().CapNhatGioiThieu(noiDung);
        else if (Request["act"].ToString() == "lien-he")
            kq = new LienHeDAO().CapNhatLienHe(noiDung);

        if (kq == 0)
            lbThongBao.Text = "Cập nhật không thành công!";
        else
            lbThongBao.Text = "Cập nhật thành công!";
    }

    protected void grvDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
        DanhMuc dm = new DanhMucDAO().HienThiChiTietDanhMuc(idDanhMuc);
        txtTenDM.Text = dm.TenDanhMuc;
        
        grvDanhMucCon.DataSource = new DanhMucConDAO().HienThiDanhMucCon(1, idDanhMuc);
        grvDanhMucCon.DataBind();

    }
    protected void grvDanhMuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvDanhMuc.PageIndex = e.NewPageIndex;
        if (Request.QueryString.Count > 0)
        {
            grvDanhMuc.DataSource = new DanhMucDAO().HienThiDanhMuc();
            grvDanhMuc.DataBind();
        }
    }
    protected void btnTaoMoi_Click(object sender, EventArgs e)
    {
        lbBaoLoi.Text = "";
        string tenDM = txtTenDM.Text;
        int kq = new DanhMucDAO().ThemDanhMuc(tenDM);
        if (kq == 0)
            lbBaoLoi.Text = "Thêm không thành công!";
        grvDanhMuc.DataSource = new DanhMucDAO().HienThiDanhMuc();
        grvDanhMuc.DataBind();
    }
    protected void btnChinhSua_Click(object sender, EventArgs e)
    {
        lbBaoLoi.Text = "";
        int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
        string tenDM = txtTenDM.Text;
        int kq = new DanhMucDAO().CapNhatDanhMuc(idDanhMuc, tenDM);
        if (kq == 0)
            lbBaoLoi.Text = "Cập nhật không thành công!";
        grvDanhMuc.DataSource = new DanhMucDAO().HienThiDanhMuc();
        grvDanhMuc.DataBind();
    }

    protected void btnXoa_Click(object sender, EventArgs e)
    {
        lbBaoLoi.Text = "";
        int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
        int kq = new DanhMucDAO().XoaDanhMuc(idDanhMuc);
        if (kq == 0)
            lbBaoLoi.Text = "Xóa không thành công!";
        else
            lbBaoLoi.Text = "Xóa thành công!";
        grvDanhMuc.DataSource = new DanhMucDAO().HienThiDanhMuc();
        grvDanhMuc.DataBind();
        txtTenDM.Text = "";
    }

    protected void grvDanhMucCon_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idDanhMucCon = int.Parse(((Label)grvDanhMucCon.SelectedRow.FindControl("lbIDDanhMucCon")).Text);
        DanhMucCon dm = new DanhMucConDAO().HienThiDanhMucCon(2, idDanhMucCon)[0];
        txtDMCon.Text = dm.TenDanhMucCon;
        edtDMCon.Text = dm.NoiDung.ToString();
        pnChiTietDMCon.Visible = true;
    }
    protected void grvDanhMucCon_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvDanhMucCon.PageIndex = e.NewPageIndex;
        if (Request.QueryString.Count > 0)
        {
            int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
            grvDanhMucCon.DataSource = new DanhMucConDAO().HienThiDanhMucCon(1, idDanhMuc);
            grvDanhMucCon.DataBind();
        }
    }
    protected void btnTaoMoiDMCon_Click(object sender, EventArgs e)
    {
        lbBaoLoi1.Text = "";
        string tenDMCon = txtDMCon.Text;
        int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
        string noiDung = edtDMCon.Text;
        string tenTat = ConvertToUnSign(tenDMCon);
        int kq = new DanhMucConDAO().ThemDanhMucCon(idDanhMuc, tenDMCon, noiDung, tenTat);
        if (kq == 0)
            lbBaoLoi1.Text = "Thêm không thành công!";
        grvDanhMucCon.DataSource = new DanhMucConDAO().HienThiDanhMucCon(1, idDanhMuc);
        grvDanhMucCon.DataBind();
    }
    public string ConvertToUnSign(string text)
    {

        for (int i = 32; i < 48; i++)
        {

            text = text.Replace(((char)i).ToString(), " ");

        }

        text = text.Replace(".", "-");

        text = text.Replace(" ", "-");

        text = text.Replace(",", "-");

        text = text.Replace(";", "-");

        text = text.Replace(":", "-");



        Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");



        string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

        return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');

    }
    protected void btnSuaDMCon_Click(object sender, EventArgs e)
    {
        lbBaoLoi1.Text = "";
        string tenDMCon = txtDMCon.Text;
        int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
        int idDanhMucCon = int.Parse(((Label)grvDanhMucCon.SelectedRow.FindControl("lbIDDanhMucCon")).Text);
        string noiDung = edtDMCon.Text;
        string tenTat = ConvertToUnSign(tenDMCon);
        int kq = new DanhMucConDAO().CapNhatDanhMucCon(idDanhMucCon, idDanhMuc, tenDMCon, noiDung, tenTat);
        if (kq == 0)
            lbBaoLoi1.Text = "Cập nhật không thành công!";
        grvDanhMucCon.DataSource = new DanhMucConDAO().HienThiDanhMucCon(1, idDanhMuc);
        grvDanhMucCon.DataBind();
    }
    protected void btnXoaDMCon_Click(object sender, EventArgs e)
    {
        lbBaoLoi1.Text = "";
        int idDanhMucCon = int.Parse(((Label)grvDanhMucCon.SelectedRow.FindControl("lbIDDanhMucCon")).Text);
        int kq = new DanhMucConDAO().XoaDanhMucCon(idDanhMucCon);
        if (kq == 0)
            lbBaoLoi1.Text = "Xóa không thành công!";
        else
            lbBaoLoi1.Text = "Xóa thành công!";
        int idDanhMuc = int.Parse(((Label)grvDanhMuc.SelectedRow.FindControl("lbIDDanhMuc")).Text);
        grvDanhMucCon.DataSource = new DanhMucConDAO().HienThiDanhMucCon(1, idDanhMuc);
        grvDanhMucCon.DataBind();
        edtDMCon.Text = "";
        txtDMCon.Text = "";
    }
    protected void grvTinTuc_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbBaoLoi2.Text = "";
        int idTinTuc = int.Parse(((Label)grvTinTuc.SelectedRow.FindControl("lbIDTinTuc")).Text);

        TinTuc tt = new TinTucDAO().HienThiChiTietTinTuc(idTinTuc)[0];
        txtTieuDe.Text = tt.TieuDe;
        edtGioiThieu.Text = tt.GioiThieu;
        edtTinTuc.Text = tt.NoiDung;
        Image1.ImageUrl = "images/tintuc/" + tt.HinhAnh;
        HiddenHinhAnh.Value = tt.HinhAnh;
    }
    protected void grvTinTuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvTinTuc.PageIndex = e.NewPageIndex;
        if (Request.QueryString.Count > 0)
        {
            grvTinTuc.DataSource = new TinTucDAO().HienThiTinTuc();
            grvTinTuc.DataBind();
        }
    }
    protected void btnTaoMoiTT_Click(object sender, EventArgs e)
    {
        TinTuc dto = new TinTuc();
        dto.IDTinTuc = 0;
        dto.GioiThieu = edtGioiThieu.Text;
        dto.NoiDung = edtTinTuc.Text.ToString();
        dto.TieuDe = txtTieuDe.Text;
        dto.NgayDang = DateTime.Now;
        string tenTat = ConvertToUnSign(dto.TieuDe);
        dto.TenTat = tenTat;
        dto.HinhAnh = "";
        if (fileTinTuc.HasFile)
        {
            if (fileTinTuc.FileName.EndsWith(".jpg") || fileTinTuc.FileName.EndsWith(".gif") || fileTinTuc.FileName.EndsWith(".bmp") || fileTinTuc.FileName.EndsWith(".png") || fileTinTuc.FileName.EndsWith(".jpeg"))
            {
                int max = new TinTucDAO().LayMaxIDTinTuc();
                if (max == -1)
                    return;
                String filename = (max + 1) + "-" + fileTinTuc.FileName; 
                dto.HinhAnh = filename;
                string path = MapPath("images/tintuc/" + filename);
                fileTinTuc.SaveAs(path);

            }
            else
            {
                lbBaoLoi2.Text = "Hình ảnh không hợp lệ";
                return;
            }
        }

        int kq = new TinTucDAO().ThemTinTuc(dto);
        if (kq == 0)
            lbBaoLoi2.Text = "Thêm không thành công!";
        grvTinTuc.DataSource = new TinTucDAO().HienThiTinTuc();
        grvTinTuc.DataBind();
    }
    protected void btnChinhSuaTT_Click(object sender, EventArgs e)
    {

        TinTuc dto = new TinTuc();
        dto.IDTinTuc = int.Parse(((Label)grvTinTuc.SelectedRow.FindControl("lbIDTinTuc")).Text);
        dto.GioiThieu = edtGioiThieu.Text;
        dto.NoiDung = edtTinTuc.Text;
        dto.TieuDe = txtTieuDe.Text;
        dto.NgayDang = DateTime.Now;
        string tenTat = ConvertToUnSign(dto.TieuDe);
        dto.TenTat = tenTat;
        if (fileTinTuc.HasFile)
        {
            if (fileTinTuc.FileName.EndsWith(".jpg") || fileTinTuc.FileName.EndsWith(".gif") || fileTinTuc.FileName.EndsWith(".bmp") || fileTinTuc.FileName.EndsWith(".png") || fileTinTuc.FileName.EndsWith(".jpeg"))
            {
                int max = new TinTucDAO().LayMaxIDTinTuc();
                if (max == -1)
                    return;
                String filename = (max + 1) + "-" + fileTinTuc.FileName;
                dto.HinhAnh = filename;
                string path = MapPath("images/tintuc/" + filename);
                fileTinTuc.SaveAs(path);

            }
            else
            {
                lbBaoLoi2.Text = "Hình ảnh không hợp lệ";
                return;
            }
        }
        else
            dto.HinhAnh = HiddenHinhAnh.Value;
        int kq = new TinTucDAO().CapNhatTinTuc(dto);
        if (kq == 0)
            lbBaoLoi2.Text = "Cập nhật không thành công!";
        grvTinTuc.DataSource = new TinTucDAO().HienThiTinTuc();
        grvTinTuc.DataBind();
    }
    protected void btnXoaTT_Click(object sender, EventArgs e)
    {
        int idTinTuc = int.Parse(((Label)grvTinTuc.SelectedRow.FindControl("lbIDTinTuc")).Text);
        int kq = new TinTucDAO().XoaTinTuc(idTinTuc);
        if (kq == 0)
            lbBaoLoi2.Text = "Xóa không thành công!";
        else
            lbBaoLoi2.Text = "Xóa thành công!";
        grvTinTuc.DataSource = new TinTucDAO().HienThiTinTuc();
        grvTinTuc.DataBind();
        txtTieuDe.Text = "";
        edtGioiThieu.Text = "";
        Image1.ImageUrl = "";
        edtTinTuc.Text = "";
    }
    protected void grvSlideShow_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbBaoLoi3.Text = "";
        int idSlideShow = int.Parse(((Label)grvSlideShow.SelectedRow.FindControl("lbIDSlideShow")).Text);

        SlideShow ss = new SlideShowDAO().HienThiChiTietSlideShow(idSlideShow);
        txtChuThich.Text = ss.ChuThich;
        imgSlideShow.ImageUrl = "imgs/" + ss.TenHinh;
        HiddenHinhAnh.Value = ss.TenHinh;
    }
    protected void grvSlideShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvSlideShow.PageIndex = e.NewPageIndex;
        if (Request.QueryString.Count > 0)
        {
            grvSlideShow.DataSource = new SlideShowDAO().HienThiSlideShow();
            grvSlideShow.DataBind();
        }
    }
    //ADD HIEU-HV 30/10/2011 START
    protected void grvLienKetWeb_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetDataItem();
    }
    protected void grvLienKetWeb_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvLienKetWeb.PageIndex = e.NewPageIndex;
        if (Request.QueryString.Count > 0)
        {
            grvLienKetWeb.DataSource = new LienKetWebDAO().HienThiLienKetWeb();
            grvLienKetWeb.DataBind();
        }        
    }
    protected void btnTaoMoiLienKetWeb_Click(object sender, EventArgs e)
    {
        lbBaoLoi4.Text = "";
        LienKetWeb dto = new LienKetWeb();
        dto.IDLienKet = 0;
        dto.UrlLienKet = txtUrl.Text;
        dto.ChuThich = txtChuThichLienKet.Text;
        if (fileLienKetWeb.HasFile)
        {
            if (fileLienKetWeb.FileName.EndsWith(".jpg") || fileLienKetWeb.FileName.EndsWith(".gif") || fileLienKetWeb.FileName.EndsWith(".bmp") || fileLienKetWeb.FileName.EndsWith(".png") || fileLienKetWeb.FileName.EndsWith(".jpeg"))
            {
                int max = new LienKetWebDAO().LayMaxIDLienKetWeb();
                if (max == -1)
                    return;
                String filename = (max + 1) + "-" + fileLienKetWeb.FileName;
                dto.HinhAnh = filename;
                string path = MapPath("images/lienket/" + filename);
                fileLienKetWeb.SaveAs(path);

            }
            else
            {
                lbBaoLoi4.Text = "Hình ảnh không hợp lệ";
                return;
            }
        }        
        CheckBox cb = (CheckBox)UpdatePanel8.FindControl("cbHienThiLienKetWeb");
        if (cb.Checked == true)
            dto.HienThi = 1;
        else
            dto.HienThi = 0;
        int kq = new LienKetWebDAO().CapNhatLienKetWeb(dto);
        if (kq == 0)
            lbBaoLoi4.Text = "Thêm không thành công!";
        else
            lbBaoLoi4.Text = "Thêm thành công!";
        grvLienKetWeb.DataSource = new LienKetWebDAO().HienThiLienKetWeb();
        grvLienKetWeb.DataBind();
    }
    protected void btnSuaLienKetWeb_Click(object sender, EventArgs e)
    {
        lbBaoLoi4.Text = "";
        LienKetWeb dto = new LienKetWeb();
        dto.IDLienKet = int.Parse(((Label)grvLienKetWeb.SelectedRow.FindControl("lbIDLienKetWeb")).Text);       
        dto.UrlLienKet = txtUrl.Text;
        dto.ChuThich = txtChuThichLienKet.Text;        
        if (fileLienKetWeb.HasFile)
        {
            if (fileLienKetWeb.FileName.EndsWith(".jpg") || fileLienKetWeb.FileName.EndsWith(".gif") || fileLienKetWeb.FileName.EndsWith(".bmp") || fileLienKetWeb.FileName.EndsWith(".png") || fileLienKetWeb.FileName.EndsWith(".jpeg"))
            {
                int max = new LienKetWebDAO().LayMaxIDLienKetWeb();
                if (max == -1)
                    return;
                String filename = (max + 1) + "-" + fileLienKetWeb.FileName;
                dto.HinhAnh = filename;
                string path = MapPath("images/lienket/" + filename);
                fileLienKetWeb.SaveAs(path);

            }
            else
            {
                lbBaoLoi4.Text = "Hình ảnh không hợp lệ";
                return;
            }
        }
        else
            dto.HinhAnh = hdLienKetWeb.Value;
        CheckBox cb = (CheckBox)UpdatePanel8.FindControl("cbHienThiLienKetWeb");
        if (cb.Checked == true)
            dto.HienThi = 1;
        else
            dto.HienThi = 0;
        int kq = new LienKetWebDAO().CapNhatLienKetWeb(dto);
        if (kq == 0)
            lbBaoLoi4.Text = "Cập nhật không thành công!";
        else
            lbBaoLoi4.Text = "Cập nhật thành công!";
        grvLienKetWeb.DataSource = new LienKetWebDAO().HienThiLienKetWeb();
        grvLienKetWeb.DataBind();
        SetDataItem();
    }
    protected void btnXoaLienKetWeb_Click(object sender, EventArgs e)
    {
        int idLienKetWeb = int.Parse(((Label)grvLienKetWeb.SelectedRow.FindControl("lbIDLienKetWeb")).Text);
        int kq = new LienKetWebDAO().XoaLienKetWeb(idLienKetWeb);
        if (kq == 0)
            lbBaoLoi4.Text = "Xóa không thành công!";
        else
            lbBaoLoi4.Text = "Xóa thành công!";
        grvLienKetWeb.DataSource = new LienKetWebDAO().HienThiLienKetWeb();
        grvLienKetWeb.DataBind();        
        imgLienKetWeb.ImageUrl = "";
        hdLienKetWeb.Value = "";
        txtUrl.Text = "";
        txtChuThichLienKet.Text = "";
        CheckBox cb = (CheckBox)UpdatePanel8.FindControl("cbHienThiLienKetWeb");
        cb.Checked = false;
    }
    void SetDataItem()
    {
        lbBaoLoi4.Text = "";
        int idLienKetWeb = int.Parse(((Label)grvLienKetWeb.SelectedRow.FindControl("lbIDLienKetWeb")).Text);
        LienKetWeb linkWeb = new LienKetWebDAO().HienThiChiTietLienKetWeb(idLienKetWeb);

        imgLienKetWeb.ImageUrl = "images/lienket/" + linkWeb.HinhAnh;
        txtUrl.Text = linkWeb.UrlLienKet;
        txtChuThichLienKet.Text = linkWeb.ChuThich;
        hdLienKetWeb.Value = linkWeb.HinhAnh;
        CheckBox cb = (CheckBox)UpdatePanel8.FindControl("cbHienThiLienKetWeb");
        if (linkWeb.HienThi == 1)
            cb.Checked = true;
        else
            cb.Checked = false;
    }
    //ADD HIEU-HV 30/10/2011 END
}